package org.example.spring.factory;

import org.example.spring.annotation.MyComponentScan;
import org.example.spring.config.MyGenericBeanDefinition;
import org.example.spring.util.NameGenerator;

public class MyAnnotationBeanDefinitionReader extends MyGeneralBeanDefinitionReader {

    public MyAnnotationBeanDefinitionReader(MyBeanFactory beanFactory) {
        super(beanFactory);
    }

    public void registerBeanDef(Class... annotationClass) {
        for (Class clazz : annotationClass) {
            registerBeanDefinition(clazz);
        }

    }

    private void registerBeanDefinition(Class annotationClass) {
        MyGenericBeanDefinition beanDefinition = new MyGenericBeanDefinition();
        beanDefinition.setBeanClass(annotationClass);

        // 生成bean的名称
        String beanName = NameGenerator.nameGenerator(annotationClass.getSimpleName());
        beanDefinition.setBeanName(beanName);

        myBeanFactory.registerBeanDefinition(beanName, beanDefinition);


        if (annotationClass.isAnnotationPresent(MyComponentScan.class)) {
            MyComponentScan myComponentScan = (MyComponentScan) annotationClass.getDeclaredAnnotation(MyComponentScan.class);

            String basePackages = myComponentScan.basePackages();

            try {
                doScannerPackage(basePackages);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
